package processes;

import to.MaterialTO;
import to.ProductTO;
import util.DateUtil;
import util.MaterialParser;
import util.ProductParser;

public class SewingMachine extends Machine{
	public SewingMachine(MachineManager machineManager, String machineId) {
		super(3000,2000, machineManager, machineId);
		setLineName("Sewing Machine");
	}

	@Override
	public void beforeManufacture() {
		MaterialParser mp = MaterialParser.getInstance();
		MaterialTO thread = mp.readMaterial("M2");
		int size = getWork().getSize();
		thread.setQuantity(thread.getQuantity()-size);
		mp.writeMaterial(thread);
		
		int countButtons = getWork().getCountButtons();
		if(countButtons>0){
			MaterialTO button = mp.readMaterial("M3");
			button.setQuantity(button.getQuantity()-countButtons);
			mp.writeMaterial(button);
		}
		mp.returnKey();
	}
	
	@Override
	public void afterManufacture() {
		currentProduct.setSewingQuality(generateQuality());
		currentProduct.setSewingDate(DateUtil.getInstance().getCurrentTimeString());
		currentProduct.setSewingId(getMachineId());
		currentProduct.setSewingTime((int)(DateUtil.getInstance().getCurrentTimeLong()-getStartTime()));
		ProductParser pp = ProductParser.getInstance();
		pp.writeProduct(machineManager.getNextDir(), currentProduct);
	}
}
